﻿using Volo.Abp.Domain;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Shared;

namespace SqlSugar
{
    public static class SqlSugarCoreExtensions
    {
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="newQuery"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static async Task<List<T>> ToPageListAsync<T>(this ISugarQueryable<T> newQuery, PageDTO page = null)
        {
            List<T> result = new List<T>();
            //如果传了页码，就进行分页展示
            if (page != null)
            {
                var refcount = new RefAsync<int>(0);
                var data = await newQuery.ToPageListAsync(page.PageIndex, page.PageDataCount, refcount);

                result = data;
                page.Count = refcount;
            }
            else
            {
                result = await newQuery.ToListAsync();
                page = new PageDTO();
            }
            return result;
        }
    }
}
